//
//  AppDelegate.m
//  DuoBaoKnigh
//
//  Created by aReu on 16/8/25.
//  Copyright © 2016年 aReu. All rights reserved.
//

#import "AppDelegate.h"
#import "IQKeyboardManager.h"
#import "PYSearchViewController.h"
#import "aRquestModel.h"
#import "MJRefreshConst.h"
#import "STViewController.h"
#import "aRquestModel.h"
#import "aRLaughtIV.h"
#import "ISFIRVersionCheck.h"
#import "AppDeleViewModel.h"

@interface AppDelegate () <PYSearchViewControllerDelegate>

@property (nonatomic,copy)  NSString            *starUrl;
@property (nonatomic,copy)  NSMutableArray      *starArr;
@property (nonatomic,strong)UIViewController    *rVc;
@property (nonatomic,strong)PYSearchViewController *svc;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
//    [NSThread sleepForTimeInterval:3.0f];
    _starArr = @[].mutableCopy;
//    [aRquestModel initializePicByName:@"刘德华" WithBlock:^(id obj1) {
//        MJWeakSelf
//        if (obj1) weakSelf.starArr = obj1;
//    }];
    
    [self initSetting];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window setRootViewController:[UIViewController new]];
    [self.window makeKeyAndVisible];
    [self bingModel];
    return YES;
}

-(void)bingModel{
    AppDeleViewModel *model = [AppDeleViewModel new];
    @weakify(self);
    [model.bbSinal subscribeNext:^(id x) {
        @strongify(self);
        [self refreshVC:x];
    }];
    
    [RACObserve(self.svc, currentText) subscribeNext:^(id x) {
        if (x == nil)return
        [model requestData:x];
    }];
    
    [RACObserve(model, searchArray) subscribeNext:^(id x) {
        self.svc.searchSuggestions = x;
    }];
    
    [RACObserve(model, searchArray) subscribeNext:^(id x) {
        self.svc.searchSuggestions = x;
    }];
    
}



-(void)initSetting{
    [aRAPPLICATION setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}

-(void)refreshVC:(id)x {
    self.svc = [PYSearchViewController searchViewControllerWithHotSearches:x                                                                   searchBarPlaceholder:x[0] didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
        STViewController *vc = [STViewController new];
        vc.songName = searchText;
        vc.type = STControllerTypeNormal;
        [searchViewController.navigationController pushViewController:vc animated:YES];
    } bgImg:nil];
    self.svc.view.frame = self.window.frame;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.svc];
    [self.window setRootViewController:nav];
    [self.window makeKeyAndVisible];
}


//-(void)applicationDidEnterBackground:(UIApplication *)application{
//    self.rVc = self.window.rootViewController;
//    self.window.rootViewController = nil;
//    self.window.backgroundColor = [UIColor blackColor];
//}
//
//-(void)applicationWillEnterForeground:(UIApplication *)application{
//    if (self.rVc) {
//        self.window.backgroundColor = [UIColor clearColor];
//        self.window.rootViewController = self.rVc;
//    }
//    if (_starArr.count > 0) {
//        aRLaughtIV * adView = [[aRLaughtIV alloc]initWithWindow:self.window andType:1 andImgUrl:_starArr[arc4random() % _starArr.count]];
//        adView.adTime = 2.0f;
//        adView.clickBlock = ^(NSInteger tag){
//        };
//    }
//}
@end

